#coding=utf-8
from Core import MongoDB, MySQLDB
import datetime

class MongoDBPipeline(object):

    def __init__(self, mongo_addr, mongo_port):
        self.mongo_addr = mongo_addr
        self.mongo_port = mongo_port


    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            mongo_addr=crawler.settings.get('DATABASE_ADDR'),
            mongo_port=crawler.settings.get('DATABASE_PORT')
        )


    def open_spider(self, spider):
        self.database = MongoDB.MongoDB(self.mongo_addr, str(self.mongo_port))


    def close_spider(self, spider):
        self.database.Close()


    def process_item(self, item, spider):

        if item["Type"] == "News":
            # print("Save To DataBase:" + item["Title"], item["DateTime"])
            item["Created_Time"] = datetime.datetime.now()
            item["Modified_Time"] = item["Created_Time"]
            self.database.Upsert("Text", "News", {"URL": item["URL"]}, dict(item))

        #if item['DataBaseName'] and item["CollectionName"]:
        #    databaseName = item['DataBaseName']
        #    collectionName = item["CollectionName"]
        #    del item['DataBaseName']
        #    del item['CollectionName']
        #    self.database.upsert(databaseName, collectionName, {"Key": item["Key"]}, item)
        else:
            pass
            # raise DropItem("Missing price in %s" % item)

        pass
        # return item


class MySQLDBPipeline(object):

    def __init__(self, addr, port, user, password):
        self.addr = addr
        self.port = port
        self.user = user
        self.password = password


    @classmethod
    def from_crawler(cls, crawler):
        return cls(
            addr=crawler.settings.get('DATABASE_ADDR'),
            port=crawler.settings.get('DATABASE_PORT'),
            user=crawler.settings.get('DATABASE_USER'),
            password=crawler.settings.get('DATABASE_PASS')
        )


    def open_spider(self, spider):
        self.database = MySQLDB.MySQLDB(self.addr, str(self.port), self.user, self.password)


    def close_spider(self, spider):
        self.database.Close()


    def process_item(self, item, spider):

        if item.get("Islist"):
            databaseName = item["DataBaseName"]
            tableName = item["TableName"]
            self.database.Upsert_Many(databaseName, tableName, [], item.get("Islist"))
            return None

        item["Created_Time"] = datetime.datetime.now()
        if item.get("Type") !=None:
            if item["Type"] == "News":
                # print("Save To DataBase:" + item["Title"], item["DateTime"])
                self.database.Upsert("Text", "News", {"URL": item["URL"]}, dict(item))
        else:
            databaseName = item["DataBaseName"]
            tableName = item["TableName"]
            del item["DataBaseName"]
            del item["TableName"]
            self.database.Upsert(databaseName, tableName, {}, dict(item))
            pass
            # raise DropItem("Missing price in %s" % item)

        # return item